package com.cwt_taxi_xa;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.cwt_taxi_xa.model.QuestionInfo;
import com.cwt_taxi_xa.protocol.DispatchCenterUsers;
import com.cwt_taxi_xa.protocol.Encoder;
import com.cwt_taxi_xa.util.StringUtil;
import com.cwt_taxi_xa.util.ToastUtils;
/**
 * 抢答界面
 * @author ferch
 *
 */
public class QuestionActivity extends CWTBaseActivity implements OnClickListener{
	private static final String TAG = "EventActivity";
	public TextView message_info,cancel,submit,time;
	public static QuestionActivity instance = null;
	public static QuestionInfo info;
	private RadioGroup rg_answer;
	private int[] answerId;
	//	private QuestionBiz biz;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_question);

		instance=QuestionActivity.this;
		//		biz = new QuestionBiz(this);

		info = (QuestionInfo) getIntent().getSerializableExtra("info");
		setupViews();
		addListener();
		initData();
	}

	private void initData() {
		// TODO Auto-generated method stub
		String aa = info.getFlag();
		if(StringUtil.isNull(aa)){
			message_info.setText(info.getText());
		}else{
			int alarmFlag = Integer.parseInt(String.valueOf(aa.charAt(0)));
			if(alarmFlag == 1){
				message_info.setText("（紧急）"+info.getText());
			}else{
				message_info.setText(info.getText());
			}
		}

		String answer = info.getAnswer();
		if(!StringUtil.isNull(answer)){
			String[] answerArr = answer.split(";");
			answerId = new int[answerArr.length];
			for(int i = 0;i<answerArr.length;i++){
				String[] answerOption = answerArr[i].split(",");
				answerId[i] = Integer.parseInt(answerOption[0]);
				String answerOptionText = answerOption[1];

				RadioButton button = new RadioButton(this);
				button.setId(i);
				button.setText("   "+answerOptionText);
				button.setTextSize(30f);
				rg_answer.addView(button, i);
				LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
				params.setMargins(0, 0, 0, 10);
				button.setLayoutParams(params);
				if(info.getIsDone() == 1){
					if(info.getAnswerId() == answerId[i]){
						button.setChecked(true);
					}
				}else{
					if(i ==0){
						button.setChecked(true);
					}
				}
			}
		}
		if(info.getIsDone() == 1){
			submit.setText("答案已提交");
			submit.setClickable(false);
		}
	}

	private void setupViews() {
		// TODO Auto-generated method stub
		message_info = (TextView)this.findViewById(R.id.message_info);
		cancel = (TextView)this.findViewById(R.id.cancel);
		time = (TextView)this.findViewById(R.id.time);
		submit = (TextView)this.findViewById(R.id.submit);
		rg_answer = (RadioGroup)this.findViewById(R.id.rg_answer);
	}

	private void addListener() {
		// TODO Auto-generated method stub
		submit.setOnClickListener(this);
		cancel.setOnClickListener(this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
	@Override
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.submit:
				if(info.getIsDone() == 1){
					ToastUtils.showToastForCustom("您已经提交过答案，无需重复提交");
				}else{
					int selectedId = answerId[rg_answer.getCheckedRadioButtonId()];
					DispatchCenterUsers.witeMsg(Encoder.questionAnswer(info.getId(), selectedId), 0);
					info.setAnswerId(selectedId);
				}
				break;
			case R.id.cancel:
				QuestionActivity.this.finish();
				break;

			default:
				break;
		}
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			int what = msg.what;
			if (what == 102) {
				int obj = (Integer) msg.obj;
				if(obj < 0){
					finish();
				}
				time.setText(""+obj);
			}
		}
	};

	@Override
	protected void onDestroy() {
		super.onDestroy();
		instance = null;
	}

	@Override
	public void setStatus(int index, boolean status) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sendMsg(int type, Object obj) {
		// TODO Auto-generated method stub

	}
}
